﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EnCryptDecrypt;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        clear();
        lblInfo.Text = "";
    }
    protected void btnDangky_Click(object sender, EventArgs e)
    {
        try
        {
            string password = Md5(txtPassword.Text);
            string makichhoat = generatecode(EnCryptDecrypt.CryptorEngine.Encrypt(txtEmail.Text.Trim(), true)).Replace("+", "").Replace("-", "").Trim();
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddUsers";
            Dal.ParameterCollection = new string[4] { "@TenHienThi", "@UserEmail", "@MatKhau", "@MaKichHoat" };
            Dal.ValueCollection = new string[4] { txtDisplayName.Text, txtEmail.Text, password, makichhoat };
            Dal.ReturnValueParameter = "@ReturnValue";
            Dal.ExecuteNonQuery();
            if (Dal.ReturnValue == 0)
                lblInfo.Text = "Email đã tồn tại";
            else
            {
                string activeaccount = "http://phoamthuc.net/User/Kich-hoat-tai-khoan.aspx";
                string mailbody = "";
                mailbody += "<b>Chào mừng bạn trở thành thành viên của Phố Ẩm Thực</b><br />";
                mailbody += "Bạn vui lòng click vào <a href=" + activeaccount + "?Makichhoat=" + makichhoat + ">đường dẫn</a> này để kích hoạt tài khoản <br />";
                SendMail("mail.phoamthuc.net", 25, txtEmail.Text, "info@phoamthuc.net", "Chào mừng đến với Phố Ẩm Thực", mailbody, "info@phoamthuc.net", "iloveyou");
                clear();
                lblInfo.ForeColor = System.Drawing.Color.AliceBlue;
                lblInfo.Text = "Đăng ký thành công, bạn vui lòng xác nhận Email";
            }

            Dal.Close();
            Dal.Dispose();
        }
        catch
        {
            lblInfo.Text = "Có lỗi xảy ra, vui lòng đăng ký lại";
            clear();
        }

    }

    protected void clear()
    {
        txtDisplayName.Text = "";
        txtEmail.Text = "";
        txtPassword.Text = "";
        txtRePassword.Text = "";
    }
    protected string generatecode(string st)
    {
        //65-90
        string result;
        result = "";
        Random rd = new Random();
        for (int i = 0; i <= 4; i++)
        {
            result += (char)rd.Next(65, 90);
        }
        result += st;
        for (int i = 0; i <= 4; i++)
        {
            result += (char)rd.Next(65, 90);
        }
        return result;
    }

    protected bool SendMail(String SMTPServer, int SMTPPort, String MailTo, String MailFrom, String MailSubject, String MailBody, String MailCredentials, String PasswordCredentials)
    {
        bool SendOK = false;
        try
        {
            if (!String.IsNullOrEmpty(MailTo) && !String.IsNullOrEmpty(MailFrom))
            {
                SmtpClient smtp = new SmtpClient(SMTPServer, SMTPPort);
                smtp.Credentials = new System.Net.NetworkCredential(MailCredentials, PasswordCredentials);
                MailAddress to = new MailAddress(MailTo);
                MailAddress from = new MailAddress(MailFrom);
                MailMessage message = new MailMessage(from, to);
                message.Subject = MailSubject;
                message.Body = MailBody;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                smtp.Send(message);
                SendOK = true;
            }
        }
        catch (Exception objEx)
        {
            lblInfo.Text = objEx.Message;
        }
        return SendOK;
    }
    public static string Md5(string strPass)
    {
        try
        {
            MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strPass));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        catch (Exception objEx)
        {
        }
        return string.Empty;
    }
}
